function ajax(opt) {

    var _default = {
      method: "get",
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      data: {},
      success: function (data, xhr) {},
      error: function (err) {},
    };
  
    
    Object.assign(_default, opt);
  
    var xhr = new XMLHttpRequest();
  
 
    var tmp = [];
    for (var key in _default.data) {
      tmp.push(key + "=" + _default.data[key]);
    }
    var qs = tmp.join("&");
  
    
    if (_default.method.toUpperCase() === "GET") {
      _default.url = qs ? _default.url + "?" + qs : _default.url;
      xhr.open("GET", _default.url);
      xhr.send();
    }
  
    // 进行post封装
    if (_default.method.toUpperCase() === "POST") {

      xhr.open("POST", _default.url);

      for (var key in _default.headers) {
        xhr.setRequestHeader(key, _default.headers[key]);
      }

      if (_default.headers["Content-Type"].includes("application/json")) {
        xhr.send(JSON.stringify(_default.data));
      } else {
        xhr.send(qs);
      }
    }
  
    return new Promise((resolve, reject) => {

      xhr.onload = function () {

        if (
          xhr.status.toString().startsWith("4") ||
          xhr.status.toString().startsWith("5")
        ) {
            reject(xhr);
          return _default.error(xhr);
        }
  
        var result = xhr.responseText;
  
        if (xhr.getResponseHeader("content-type").includes("application/json")) {
          result = JSON.parse(result);
        } 
        resolve(result)
        return _default.success(result);
      };
  
      xhr.onerror = function () {
        reject(xhr)
        _default.error(xhr);
      };
    });
  }